package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.CheckBoxEntityBean;
import com.sp.scmdb.bean.MenuEntityBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Role;
import com.sp.scmdb.service.RoleService;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.bean.RoleBean;

/**
 * 角色管理Action
 * @author maweiyue
 *
 */
@Controller("roleAction")
@Scope("prototype")
public class RoleAction extends BaseAction<RoleBean> {

	/** */
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(RoleAction.class);

	/** 角色Service */
	private RoleService roleService;

	/** 角色service设置 */
	public void setRoleService(RoleService service) {
		this.roleService = service;
	}
	
	/**
	 * 全查询方法
	 * @return 视图名称
	 */
	public String findAll() {		
		try {
			// 调用查询，获取所有的角色列表
			List<RoleBean> roleList = roleService.findAll();
			entity.setAllList(roleList);
			entity.setList(roleList.subList(entity.getCurrentRow(), entity.getLastRow()));		

			// 页面”添加“，”修改“，”删除“按钮的权限设置
			// 调用BaseAction的button(String urlAdd, String urlEdit, String urlDel)方法
			this.button("Super.Popup(5,'添加角色','/SCMDB/roleAction!findMenu.action','',600,500)", 
					"Super.editData('/SCMDB/roleAction!findRole.action?id=',600,500)", 
					"Super.delData('/SCMDB/roleAction!delRole.action?id=')");
		} catch(RuntimeException e){			
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("findAll fail!reason:" + e.getMessage());
			return "error";
		}
		
		return SUCCESS;
	}
	
	/**
	 * 取得所有的菜单及部门列表
	 */
	public String findMenu() {

		// 当前用户信息
		
		UserBean user = (UserBean)session.get("user");
				
		try {
			// 页面显示的所有的菜单
			List<MenuEntityBean> liseMenuEntityBean = roleService.queryMenu(user);
			entity.setLiseMenuEntityBean(liseMenuEntityBean);
			
			// 页面显示的所有部门
			List<CheckBoxEntityBean> listDepartment = roleService.queryDepartment(user);
			entity.setDepartmentList(listDepartment);
		} catch(RuntimeException e){			
			log.error("findMenu fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("findMenu fail!reason:" + e.getMessage());
			return "error";
		}
		
		return "newSucc";
	}

	/**
	 * 添加角色
	 */
	public String addRole() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = roleService.newRole(getEntity(), user);
			messageSession(result,"角色");
		} catch(RuntimeException e){			
			log.error("addRole fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("addRole fail!reason:" + e.getMessage());
			return "error";
		}
		
		return "commitSucc";
	}

	/**
	 * 跳转至编辑页面
	 */
	public String findRole() {
		// 当前用户信息
		UserBean user = (UserBean)session.get("user");
		try {
			String id = (String)request.getParameter("id");
			entity = roleService.findRoleById(Integer.parseInt(id), user);
		} catch(RuntimeException e){			
			log.error("findRole fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("findRole fail!reason:" + e.getMessage());
			return "error";
		}
		
		return "editSucc";
	}

	/**
	 * 更新角色
	 */
	@SuppressWarnings("unchecked")
	public String editRole() {
		try {
			UserBean user = (UserBean) session.get("user");
			// 修改角色
			String result = roleService.editRole(getEntity(), user);
			messageSession(result,"角色");
			// 修改角色之后，根据当前用户的id，获取新的角色，并放入session
			Role role = roleService.getById(user.getRole().getId());
			user.setRole(role);
			session.put("user", user);
			
		} catch(RuntimeException e){			
			log.error("editRole fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("editRole fail!reason:" + e.getMessage());
			return "error";
		}

		return "commitSucc";
	}

	/**
	 * 删除角色
	 */
	public String delRole() {
		try {
			UserBean user = (UserBean) session.get("user");
			String id = (String)request.getParameter("id");
			String result = roleService.delRole(id, user);
			if ("10".equals(result)) {
				messageSession("0","超级管理员角色不能删除！");
			} else {
				messageSession(result,"角色");
			}			
		} catch(RuntimeException e){			
			log.error("delRole fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("delRole fail!reason:" + e.getMessage());
			return "error";
		}
		
		return "commitSucc";
	}
}
